package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.ActivityRecommendBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class RecommendActivity extends BaseAc<ActivityRecommendBinding> {

    protected RecommendAdapter recommendAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_recommend;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivRecommendBack.setOnClickListener((v) -> onBackPressed());
        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);
        recommendAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
        mDataBinding.refreshRecommendItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshRecommendItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshRecommendItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshRecommendItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshRecommendItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getData() {
        //获取数据
        StkResApi.getTagResourceList(null, AppConfig.RECOMMEND_DATA, StkApi.createParamMap(0, 10), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (page == 1) {
                                recommendAdapter.setList(result);
                            } else {
                                recommendAdapter.addData(result);
                            }
                        }
                    }
                });
    }

}